<?php
namespace Application\Controller\Auth;

use Application\Service\PasswordService;
use Zend\Authentication\Adapter\AdapterInterface;
use Zend\Authentication\Result;
use Application\Controller\Auth\UserIdentity;
use Zend\ServiceManager\ServiceManagerAwareInterface;
use Application\Model\Dao\UserDao;

class AuthAdapter implements AdapterInterface, ServiceManagerAwareInterface
{

    private $userName;

    private $password;
    
    private $serviceMgr;

    function __construct($userName, $password)
    {
        $this->password = $password;
        $this->userName = $userName;
    }
    
    /*
     * (non-PHPdoc)
     * @see \Zend\Authentication\Adapter\AdapterInterface::authenticate()
     */
    public function authenticate()
    {
        $userDao = new UserDao();
        $user =  $userDao->getUserByEmailWithPassword($this->userName);


        if($user != null && $user->blocked == null){

            $ps = new PasswordService();
            if($ps->validate_password($this->password, $user->password)){
                $user->password = '';
                $result = new Result(Result::SUCCESS, $user);
            }else{
                $result = new Result(Result::FAILURE, null);
            }
        } else if($user != null && $user->blocked != null){
        	$result = new Result(Result::FAILURE, 'Blocked');
        } else {
            $result = new Result(Result::FAILURE, null);
        }
        return $result;
    }
    
	/* (non-PHPdoc)
     * @see \Zend\ServiceManager\ServiceManagerAwareInterface::setServiceManager()
     */
    public function setServiceManager(\Zend\ServiceManager\ServiceManager $serviceManager)
    {
        $this->serviceMgr = $serviceManager;
    }

}
